package com.atguigu.springcloud.extend.impl;

import cn.hutool.json.JSONArray;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.springcloud.exception.impl.CustomAssert;
import com.atguigu.springcloud.extend.RedisService;
import com.atguigu.springcloud.extend.StringRedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 默认redis实现
 * @date 2021/10/18 17:15
 */
public class DefaultRedisServiceImpl implements RedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void setString(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void setString(String key, String value, long time) {
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    @Override
    public String getString(String key) {
        return key == null ? null : (String) redisTemplate.opsForValue().get(key);
    }

    @Override
    public void setValue(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void setValue(String key, Object value, long time) {
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    @Override
    public Object getValue(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    @Override
    public <T> T getValue(String key, Class<T> clazz) {
        if (key == null) {
            return null;
        }
        Object object = redisTemplate.opsForValue().get(key);
        CustomAssert.notTrue(!(object instanceof JSONArray), "值类型为List");
        if (object instanceof JSONObject) {
            return ((JSONObject) object).toJavaObject(clazz);
        }
        return (T) object;

    }

    ///
    @Override
    public Object getObject(String key, Class<?> clazz) {
        return null;
    }

    @Override
    public boolean expire(String key, long expire) {
        return false;
    }

    @Override
    public void remove(String key) {

    }

    @Override
    public void put(String key, String hashKey, Object value) {

    }

    @Override
    public void putAll(String key, Map<String, Object> map) {

    }

    @Override
    public void putDto(String key, Object dto) {

    }

    @Override
    public Long deleteHashKeys(String key, String... hashKeys) {
        return null;
    }

    @Override
    public Object get(String key, String hashKey) {
        return null;
    }

    @Override
    public <R> R getDto(String key, Class<R> r) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(String key) {
        return null;
    }

    @Override
    public Set<String> hashKeys(String key) {
        return null;
    }

    @Override
    public List<Object> hashValues(String key) {
        return null;
    }

    @Override
    public boolean getLock(String key, String unLockId, long time) {
        return redisTemplate.setNx(key, unLockId, time);
    }

    @Override
    public void unLock(String key, String unLockId) {
        redisTemplate.removeNx(key, unLockId);
    }

    @Override
    public long incr(String key, long delta) {
        return 0;
    }

    @Override
    public long decr(String key, long delta) {
        return 0;
    }

    @Override
    public double hincr(String key, String item, double by) {
        return 0;
    }

    @Override
    public double hdecr(String key, String item, double by) {
        return 0;
    }

    @Override
    public Set<Object> sGet(String key) {
        return null;
    }

    @Override
    public boolean sHasKey(String key, Object value) {
        return false;
    }

    @Override
    public long sSet(String key, Object[] values) {
        return 0;
    }

    @Override
    public long sGetSetSize(String key) {
        return 0;
    }

    @Override
    public long setRemove(String key, Object[] values) {
        return 0;
    }

    @Override
    public long IRemove(String key, long count, Object value) {
        return 0;
    }

    @Override
    public boolean IUpdateIndex(String key, long index, Object value) {
        return false;
    }

    @Override
    public boolean ISet(String key, List<Object> value) {
        return false;
    }

    @Override
    public boolean ISet(String key, Object value) {
        return false;
    }

    @Override
    public Object IGetIndex(String key, long index) {
        return null;
    }

    @Override
    public long IGetListSize(String key) {
        return 0;
    }

    @Override
    public List<Object> lGet(String key, long start, long end) {
        return null;
    }

    @Override
    public <T> List<T> getList(String key, Class<T> clazz) {
        return null;
    }

    @Override
    public boolean exists(String key) {
        return false;
    }
}
